﻿/*-------------------------------------------------------------------------
* CLR版本:     4.0.30319.42000
* 创建人:      song 
* 创建时间：   2024/7/11 19:26:12
* 功 能：      Track逻辑处理
*-------------------------------------------------------------------------*/

using BattleCore.FixedPoint;
using BattleCore.proto;
using System.Collections.Generic;

namespace BattleCore.logic
{
    public class skill_track_logic
    {
        private skill_track _track;

        public skill_track_logic(skill_track skill_track)
        {
            _track = skill_track;
        }

        public List<skill_clip> _clips => _track._clips;

        public List<skill_clip_logic> _clipLogics;

        public skill_track_logic()
        {
            _clipLogics = new List<skill_clip_logic>();
            foreach (var clip in _clips)
            {
                _clipLogics.Add(new skill_clip_logic(clip));
            }
        }

        public void Tick(fp runFrame)
        {
            for (int offest = _clipLogics.Count - 1; offest >= 0; offest--)
            {
                var clipLogic = _clipLogics[offest];
                var clip = clipLogic._clip;
                //没在范围不执行
                if (runFrame > clip._endFrame || runFrame < clip._startFrame)
                {
                    continue;
                }

                if (clip._startFrame == runFrame)
                {
                    clipLogic.Enter();
                    battle.Log($"{runFrame} {clipLogic.GetType()}.Enter");
                }
                else if (runFrame > clip._startFrame && runFrame < clip._endFrame)
                {
                    clipLogic.Tick();
                }
                else if (clip._endFrame == runFrame)
                {
                    clipLogic.Exit();
                    battle.Log($"{runFrame} {clipLogic.GetType()}.Exit");
                }
            }
        }

        /// <summary>
        /// 获取当前轨道最大帧
        /// </summary>
        /// <returns></returns>
        public int GetMaxFrame()
        {
            int maxFrame = 0;
            foreach (var clip in _clips)
            {
                if (maxFrame < clip._endFrame)
                {
                    maxFrame = clip._endFrame;
                }
            }

            return maxFrame;
        }
    }
}